<template>
  <div class="outer">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="form-container">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="ruleForm.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="ruleForm.password"></el-input>
      </el-form-item>
      <el-form-item label="加密值" prop="salt">
        <el-input v-model="ruleForm.salt"></el-input>
      </el-form-item>
      <el-form-item label="姓名" prop="nickname">
        <el-input v-model="ruleForm.nickname"></el-input>
      </el-form-item>
      <el-form-item label="学院id" prop="schoolId" >
        <el-input v-model="ruleForm.schoolId"></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="sex" >
        <el-radio-group v-model="ruleForm.sex">
          <el-radio label="男">男</el-radio>
          <el-radio label="女">女</el-radio>
        </el-radio-group>
<!--        <el-input v-model="ruleForm.sex" placeholder="请输入小数"></el-input>-->
      </el-form-item>
      <el-form-item label="电话号" prop="tel">
        <el-input v-model="ruleForm.tel"></el-input>
      </el-form-item>
      <el-form-item label="班级" prop="studentClass">
        <el-input v-model="ruleForm.studentClass"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">立即添加</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "add-user",
  data() {
    return {
      // tableData: [],

      ruleForm: {
        username: '',
        password: '',
        salt: '',
        nickname: '',
        schoolId: '',
        sex: '',
        tel: '',
        studentClass: '',
      },
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
        ],
        salt: [
          {required: true, message: '请输入加密值', trigger: 'blur'}
        ],
        nickname: [
          {required: true, message: '请输入姓名', trigger: 'blur'}
        ],
        schoolId: [
          {required: true, message: '请输入学院id', trigger: 'blur'}
        ],
        sex: [
          {required: true, message: '请选择性别', trigger: 'blur'},
        ],
        tel: [
          {required: true, message: '请输入电话号码', trigger: 'blur'},
        ],
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$axios.post("/user/add", this.ruleForm,
              {
                headers: {
                  "Authorization": this.$store.getters.getToken
                }
              }).then(res => {
            this.$message({
              message: res.data.message,
              type: "success"
            });
            this.resetForm('ruleForm');
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
  created() {
    // this.getList();
  }
}
</script>

<style scoped>

.outer {

  background: #E9EEF3;
}

.form-container {
  width: 400px;
  margin: 0 auto;
  text-align: center;
}
</style>